using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OdsControllerLib;

namespace OdsController {
	public partial class form_log : Form , ILogObserver {

		public bool VisibleBellow = false;
		public bool Docked = false;
		public string DockedToFormName = "";

		#region observer
		private delegate bool UpdateDelegate(ILogSubject sender, string message, LogCategories category, DateTime timestamp);

		public bool AppendLogEntry(ILogSubject sender, string message, LogCategories category, DateTime timestamp) {
			if (InvokeRequired || rtbLog.InvokeRequired) {
				return (bool)Invoke(new UpdateDelegate(AppendLogEntry), new object[] { sender, message, category, timestamp });
			} else {
				lock (rtbLog) {
					rtbLog.SuspendLayout();

					string datetime = timestamp.ToString() + " - ";
					string prefix = "";
					string msg2 = "";
					System.Drawing.Color mycolor = new System.Drawing.Color();


					string[] amessage = message.Split('$');
					foreach (string msg in amessage) {
						if (msg.Length <= 0) continue;

						rtbLog.AppendText(datetime);
						rtbLog.Select(rtbLog.Find(datetime, RichTextBoxFinds.Reverse), datetime.Length); // this find should be used
						rtbLog.SelectionColor = System.Drawing.Color.DarkGray;

						if (msg.Equals(amessage[0]))
							msg2 = msg;
						else {
							prefix = amessage[0].Substring(0, 2);
							msg2 = prefix + " " + msg;
						}

						if (category == LogCategories.ApplicationEvents || category == LogCategories.CommunicationsReceived || category == LogCategories.CommunicationsSent || category == LogCategories.UIActions) {
							switch (message.Substring(0, 2)) {
								case "C:":
									mycolor = System.Drawing.Color.Blue;
									break;
								case "S:":
									mycolor = System.Drawing.Color.Red;
									break;
								default:
									mycolor = System.Drawing.Color.Gray;
									break;
							}
						} else if (category == LogCategories.ServiceEvents) {
							switch (message.Substring(0, 2)) {
								case "C:":
									mycolor = System.Drawing.Color.CornflowerBlue;
									break;
								case "S:":
									mycolor = System.Drawing.Color.PaleVioletRed;
									break;
								default:
									mycolor = System.Drawing.Color.Gray;
									break;
							}
						}
						rtbLog.AppendText(msg2);
						rtbLog.AppendText("\n");
						rtbLog.Select(rtbLog.Find(msg2, RichTextBoxFinds.Reverse), msg2.Length);
						rtbLog.SelectionColor = mycolor;
					}

					//  //LogControlObj.Select(LogControlObj.Rtf.Length-1,0);
					rtbLog.Select(rtbLog.Text.Length - 1, 0);
					rtbLog.ScrollToCaret();
					rtbLog.ResumeLayout(true);
					//  //_logControlObj.Parent.BringToFront();
					return true;
				}
			}
		}
		
		#endregion observer


		public form_log() {
			InitializeComponent();
			Text = Application.ProductName + " - Log";
			Icon = Properties.Resources.IconLog;
			MainObj.OdsMain.Log.Attach(this);
		}

		

		private void form_log_Move(object sender, EventArgs e) {
			this.SuspendLayout();
			Rectangle main_bounds = Owner.DesktopBounds;
			Point newpos = new Point();
			this.Docked = false;
			this.DockedToFormName = "";

			if (this.DesktopLocation.X <= (main_bounds.X + main_bounds.Width + 10) &&
				this.DesktopLocation.X >= (main_bounds.X + main_bounds.Width - 10)) {
				newpos.X = main_bounds.X + main_bounds.Width;
				newpos.Y = this.DesktopLocation.Y;
				this.Location = newpos;
				this.Docked = true;
				this.DockedToFormName = Owner.Name;
				System.Diagnostics.Debug.WriteLine("LogFormAttached - X2 on main");
			}
			if (this.DesktopLocation.X <= (main_bounds.X + 10) &&
				this.DesktopLocation.X >= (main_bounds.X - 10)) {
				newpos.X = main_bounds.X;
				newpos.Y = this.DesktopLocation.Y;
				this.Location = newpos;
				this.Docked = true;
				this.DockedToFormName = Owner.Name;
				System.Diagnostics.Debug.WriteLine("LogFormAttached - X1 on main");
			}
			if (this.DesktopLocation.Y <= (main_bounds.Y + main_bounds.Height + 10) &&
				this.DesktopLocation.Y >= (main_bounds.Y + main_bounds.Height - 10)) {
				newpos.Y = main_bounds.Y + main_bounds.Height;
				newpos.X = this.DesktopLocation.X;
				this.Location = newpos;
				this.Docked = true;
				this.DockedToFormName = Owner.Name;
				System.Diagnostics.Debug.WriteLine("LogFormAttached - Y2 on main");
			}
			if (this.DesktopLocation.Y <= (main_bounds.Y + 10) &&
				this.DesktopLocation.Y >= (main_bounds.Y - 10)) {
				newpos.Y = main_bounds.Y;
				newpos.X = this.DesktopLocation.X;
				this.Location = newpos;
				this.Docked = true;
				this.DockedToFormName = Owner.Name;
				System.Diagnostics.Debug.WriteLine("LogFormAttached - Y1 on main");
			}
			System.Diagnostics.Debug.WriteLine("LogMove - at: "+this.Location.X+","+this.Location.Y);
			this.ResumeLayout();
		}
		
		public void MinimizeToMain() {
			// if the user has not disabled animating windows...
			if (!MainObj.AnimationDisabled) {

				Win32Helper.RECT animateFrom = new Win32Helper.RECT();

				Win32Helper.GetWindowRect(this.Handle, ref animateFrom);

				Win32Helper.RECT animateTo = new Win32Helper.RECT();
				IntPtr toolbarAreaHandle = ((form_main)Owner).LogButtonWhnd;

				if (toolbarAreaHandle != IntPtr.Zero) {
					if (Win32Helper.GetWindowRect(toolbarAreaHandle, ref animateTo) == true) {
						Win32Helper.DrawAnimatedRects(this.Handle, Win32Helper.IDANI_CAPTION, ref animateFrom, ref animateTo);
					}
				}
			}
		}
		public void MaximizeFromMain() {
			// if the user has not disabled animating windows...
			if (!MainObj.AnimationDisabled) {

				Win32Helper.RECT animateTo = new Win32Helper.RECT();

				Win32Helper.GetWindowRect(this.Handle, ref animateTo);

				Win32Helper.RECT animateFrom = new Win32Helper.RECT();
				IntPtr toolbarAreaHandle = ((form_main)Owner).LogButtonWhnd;

				if (toolbarAreaHandle != IntPtr.Zero) {
					if (Win32Helper.GetWindowRect(toolbarAreaHandle, ref animateFrom) == true) {
						Win32Helper.DrawAnimatedRects(this.Handle, Win32Helper.IDANI_CAPTION, ref animateFrom, ref animateTo);
					}
				}
			}
		}

		private void form_log_FormClosing(object sender, FormClosingEventArgs e) {
			if (e.CloseReason == CloseReason.UserClosing) {
				e.Cancel = true;
				((form_main)Owner).LogVisibleToggle();
			}
		}

		private void rtbLog_LinkClicked(object sender, LinkClickedEventArgs e) {
			System.Diagnostics.Process.Start(e.LinkText);
		}

		private void clearLogToolStripMenuItem_Click(object sender, EventArgs e) {
			rtbLog.Clear();
		}

		private void form_log_VisibleChanged(object sender, EventArgs e) {
			if (!this.Visible)
				MinimizeToMain();
			//else
			//  MaximizeFromMain();
		}

	


	}
}